#
#  blog_datasource.rb
#  RPub
#
#  Created by 서구 허 on 08. 03. 12.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#

require 'osx/cocoa'
require 'blog'

DRAFT = 'DRAFTS'
PUBLISHED = 'PUBLISHED'
RECENT = 'RECENTS'

class BlogDataSource < OSX::NSObject
  def init
    super_init
    @root = nil
    self
  end
  
  def dealloc
    @root = nil
    super_dealloc
  end
  
  attr_accessor :root
  
  def outlineView_numberOfChildrenOfItem(view, item)
    item = @root if item.nil?
    item.size
  end
  
  def outlineView_isItemExpandable(view, item)
    item = @root if item.nil?
    item.respond_to?(:expandable?) ? item.expandable? : false
  end
  
  def outlineView_child_ofItem(view, index, item)
    item = @root if item.nil?
    item[index]
  end
  
  def outlineView_objectValueForTableColumn_byItem(view, column, item)
    if item.class == OutlineItem
      item.name
    elsif item.class == ArticleProxy
      item.title
    else
      item
    end
  end
end

class ArticleSummary
  def self.get_summary(param={})
    summary = OutlineItem.alloc.init
    set_default_param(param)
    
    summary << OutlineItem.create(DRAFT, ArticleProxy.convert(Article.get_drafts(param[:draft_count])))
    published = OutlineItem.create(PUBLISHED)
    recent = OutlineItem.create(RECENT, ArticleProxy.convert(Article.get_recent_articles(param[:recent_count])))
    published << recent
    summary << published
    summary
  end
  
  def self.get_drafts(param={})
    set_default_param(param)
    ArticleProxy.convert(Article.get_drafts(param[:draft_count]))
  end
  
  private
  def self.set_default_param(param)
    # update search parameters
    param[:recent_count] = 10 unless param.has_key?(:recent_count)
    param[:draft_count] = 0 unless param.has_key?(:draft_count)
    param
  end
end

class OutlineItem < OSX::NSObject
  def self.create(name='Item', children=nil)
    new_one = alloc.init
    new_one.name=name
    new_one << children unless children.nil?
    new_one
  end
  
  def init
    super_init
    @children = []
    self
  end
  
  def dealloc
    @children = nil
    super_dealloc
  end
  
  attr_accessor :name
  
  def <<(child)
    if child.class == Array
      @children += child
    else
      @children << child
    end
    self
  end

  def unshift(child)  
    if child.class == Array
      @children = child + @children
    else
      @children.unshift(child)
    end
  end
  
  def delete_at(index)
    @children.delete_at(index)
  end
  
  def delete(child)
    @children.delete(child)
  end
  
  def index(child)
    @children.index(child)
  end
  
  def size
    @children.size
  end
  
  def expandable?
    (@children.size > 0) ? true : false
  end

  def [](index)
    @children[index]
  end
  
  def move_to_top(index)
    @children.unshift(@children.delete_at(index))
  end
  
  def update(new_child)
    if new_child.class == Array
      @children = new_child
    else
      @children = [child]
    end
  end
end

class ArticleProxy < OSX::NSObject
  # should I use ActiveRecordProxy ?
  attr_accessor :title, :db_id
  
  def self.convert(article_array)
    new_array = []
    article_array.each do |a|
      wrapper = alloc.init
      wrapper.assign(a)
      new_array << wrapper
    end
    new_array
  end

  def assign(article)
    @db_id = article.id
    @title = article.title
    self
  end
  
  def ==(article)
    if article.class == ArticleProxy then
      @db_id == article.db_id
    elsif article.class == Fixnum then
      @db_id == article
    else
      false
    end
  end
end